/**
 * 功能描述
 */
define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Grid = require("editgrid");
	var Dialog = require("dialog");
	var store =require("store");
	var activeUser = store.get("user");
	var Form = require("form");
    var instance = {
        initComponent: function (params, widget) {
        	var grid = new Grid({
        	    parentNode: ".J-editgrid",
        	    model: {
        	        url: "api/welfareconsumptioncoupon/querywelfarecard",
        	        params: function() {
        	            return {};
        	        },
        	        isRadiobox: true,
        	        clickRadiobox: function(e) {
        	        	var data=grid.getSelectedData();
        	        	//点击就弹出一个对话框，然后进行操作
						var remarkForm =new Form({
							parentNode:".J-form-dialog",
							autoRender : false,//自动渲染 , 必须要 widget.set("remarkForm",remarkForm);
			        		model:{
								id:"remarkForm",
								defaultButton:false,
								layout:"2_1",
								items:[{
									name:"cardNo",
									label:"卡号",
									validate : ["required"],
									exValidate: function(value) {
						                var reg = /^[0-9]*[1-9][0-9]*$/;
						                if (!reg.test(value)) {
			                				return "卡号格式不正确！";
			                			}else {
			                				return true;
			                			}
				                    }, 
								},{
									name:"balance",
									label:"充值金额(元)",
									validate : ["required"],
									defaultValue : 4000,
					                exValidate: function(value) {
					                	var reg = /^([1-9][\d]{0,7}|0)(\.[\d]{1,2})?$/;
			                            if (!reg.test(value) || value<=0) {
			                                return "请输入正确金额！";
			                            } else {
			                                return true;
			                            }
			                        }, 
								},{
									name:"name",
									label:"会员",
									defaultValue : data[0].name,
									readonly:true,
								},{
									name:"cerCard",
									label:"会员证号",
									defaultValue : data[0].cerCard,
									readonly:true,
								},{
									name:"productName",
									label:"产品",
									defaultValue : data[0].productName,
									readonly:true,
								},{
									name:"productType",
									label:"产品类型",
									defaultValue : data[0].productType,
									readonly:true,
								},{
									name:"operator",
									label:"经办人",
									defaultValue : activeUser.name,
									readonly:true,
								},{
									name:"grantDate",
									label:"发放日期",
									type:"date",
									readonly:true,
									defaultValue : moment(new Date()).format("YYYY-MM-DD")
								},{
									name : "remark",
									label : "备注",
									"break" : true,
									defaultValue : moment(new Date()).format("YYYY年MM月DD日")+"返券"
								},{
									name:"sex",
									label: "性别",
									defaultValue:data[0].sex,
									type:"hidden"
								},{
									name:"startDate",
									label:"签约日期",
									defaultValue:moment(data[0].startDate).format("YYYY-MM-DD"),
									type:"hidden",
								},{
									name:"mobilePhone",
									label:"手机号",
									defaultValue:data[0].mobilePhone,
									type:"hidden",
								},{
									name:"version",
									label:"version",
									defaultValue:0,
									type:"hidden"
								}]
							 }
						});
						widget.set("remarkForm",remarkForm);
						Dialog.showComponent(remarkForm,{
							defaultButton:false,
							title: "新增",
							buttons :[{
								id:"save",
								text:"发卡",
									className:"btn-isave",
								handler:function(){
									if(remarkForm.valid()){
										var FormData = remarkForm.getData();
										var saveData = {};
										saveData.version = FormData.version;
										saveData.cardNo = FormData.cardNo;
										saveData.balance = FormData.balance;
										saveData.grantDate = FormData.grantDate;
										saveData.returnDate = FormData.grantDate;//这次返券日期也就是发卡日期
										saveData.remark = FormData.remark;
										saveData.status = "Effective";
										if(FormData.sex=="男"){
											saveData.sex = "Male";
										}else if(FormData.sex=="女"){
											saveData.sex = "Female";
										}
										saveData.travelContract = data[0].pktravelContract;
										saveData.operator = activeUser.pkUser;
										
										saveData.isGrant = true;
										saveData.servicePoint = widget.get("subnav").getValue("servicePoint");
										aw.saveOrUpdate("api/welfareconsumptioncoupon/savecard",aw.customParam(saveData),function(saveData){
											Dialog.close();
						          			widget.get("editgrid").refresh();
						       			});
									};
								}
							},{
								id:"close",
								text:"取消",
								handler:function(){
									Dialog.close();
									widget.get("editgrid").refresh();
								}
							}]
						});
        	        },
        	        columns: [{
        	            name: "cerCard",
        	            label: "会员证号"
        	        }, {
        	            name: "name",
        	            label: "会员"
        	        }, {
        	            name: "sex",
        	            label: "性别",
        	            format: function(value,rowData){
        	            	if(value=="Male"){
        	            		return "男";
        	            	}else if(value =="Female"){
        	            		return "女";
        	            	}else{
        	            		return null;
        	            	}
        	            }
        	        }, {
        	            name: "productName",
        	            label: "产品"
        	        }, {
        	            name: "productType",
        	            label: "产品类型",
        	        }, {
        	            name: "startDate",
        	            label: "开始日期",
        	            type:"date",
        	            format : function(value, row){
        	            	return moment(value).format("YYYY-MM-DD");
        	            },
        	        }]
        	    }
        	});
        	widget.set("editgrid", grid);
        }
    };
    
    module.exports = instance;
});